/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.generatepage.web.IdxAdvAction
   Module Description   :

   Date Created      : 2010/3/11
   Original Author   : feeling.wang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.generatepage.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.generate.entity.GenerateAdvertise;
import com.yaodian100.ec.generate.entity.ViewPmAdvertise;
import com.yaodian100.ec.generate.service.GenerateService;

/**
 * @author feeling.wang
 * 
 */
public class SearchAdvAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 4678901427925995884L;

	/** logger */
	private final Logger logger = LoggerFactory.getLogger(SearchAdvAction.class);
	private GenerateService generateService;
	private DomainService<Menu> menuService;
	private CategoryService categoryService;
	private Menu advStatusMenu; // 與 getter, setter
	private Menu advTypeMenu; // 與 getter, setter
	private Menu advIdxTypeMenu; // 與 getter, setter
	private List<GenerateAdvertise> advertises;
	private List<ViewPmAdvertise> viewPmAdvertises;
	DateFormat format = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP);
	private SimplePager pager;
	private Map<Long, String> diffDateMap;
	private List<Category> categoryRoot;

	Calendar calendar = Calendar.getInstance();
	Date today = new Date();
	String todayStr = EcConstant.PATTERN_DATE_FORMAT.format(today);

	/******* 查询PM页面参数 ********/
	private String advNameParam;
	private String startDateParam;
	private String endDateParam;
	private String advStatusParam;
	private String advTypeParam;
	/******* 查询IDX页面参数 ********/
	private String advNameIdxParam;
	private String startDateIdxParam;

	private String endDateIdxParam;
	private String advStatusIdxParam;
	private String advTypeIdxParam;

	private String categoryRootParam;

	/**
	 * 新增广告-PM
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addAdv() throws Exception {
		return "addAdv";
	}

	/**
	 * 新增广告-PM-分類頁
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addAdvCat() throws Exception {
		return "addAdvCat";
	}

	/**
	 * 新增广告-PM-馆
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addAdvGuan() throws Exception {
		return "addAdvGuan";
	}

	/**
	 * 新增广告-行销
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addIdxAdv() throws Exception {
		return "addIdxAdv";
	}

	public List<GenerateAdvertise> getAdvertises() {
		return advertises;
	}

	public Menu getAdvIdxTypeMenu() {
		return advIdxTypeMenu;
	}

	public String getAdvNameIdxParam() {
		return advNameIdxParam;
	}

	public String getAdvNameParam() {
		return advNameParam;
	}

	public String getAdvStatusIdxParam() {
		return advStatusIdxParam;
	}

	public Menu getAdvStatusMenu() {
		return advStatusMenu;
	}

	public String getAdvStatusParam() {
		return advStatusParam;
	}

	public String getAdvTypeIdxParam() {
		return advTypeIdxParam;
	}

	public Menu getAdvTypeMenu() {
		return advTypeMenu;
	}

	public String getAdvTypeParam() {
		return advTypeParam;
	}

	public List<Category> getCategoryRoot() {
		return categoryRoot;
	}

	public String getCategoryRootParam() {
		return categoryRootParam;
	}

	public Map<Long, String> getDiffDateMap() {
		return diffDateMap;
	}

	public String getEndDateIdxParam() {
		return endDateIdxParam;
	}

	public String getEndDateParam() {
		return endDateParam;
	}

	public SimplePager getPager() {
		return pager;
	}

	public String getStartDateIdxParam() {
		return startDateIdxParam;
	}

	public String getStartDateParam() {
		return startDateParam;
	}

	public List<ViewPmAdvertise> getViewPmAdvertises() {
		return viewPmAdvertises;
	}

	public void prepare() throws Exception {
		clearErrorsAndMessages();

		if (null == pager)
			pager = new SimplePager();
		pager.setPageRecord(20);// defaut 20

		if (null == advStatusMenu)
			advStatusMenu = menuService.get("Generate.AdvStatus");

		if (null == advTypeMenu)
			advTypeMenu = menuService.get("Generate.AdvType");

		if (null == advIdxTypeMenu)
			advIdxTypeMenu = menuService.get("Generate.AdvIdxType");

		if (StringUtils.isBlank(advTypeIdxParam))
			advTypeIdxParam = "IDX_MAINGOLD_V2";
		if (StringUtils.isBlank(advTypeParam))
			advTypeParam = "000_MAINGOLD_V2";

		if (null == categoryRoot)
			categoryRoot = categoryService.getChild(0l);

		logger.info("prepare ok");
	}

	/**
	 * 广告列表 - PM
	 * 
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isNotBlank(advNameParam))
			crit.addRlike("advName", advNameParam);
		if (StringUtils.isNotBlank(startDateParam))
			crit.addGe("startDate", format.parse(startDateParam + " 00:00:00"));
		if (StringUtils.isNotBlank(endDateParam))
			crit.addLe("endDate", format.parse(endDateParam + " 23:59:59"));

		if (StringUtils.isNotBlank(advStatusParam))
			crit.addEq("advStatus", advStatusParam);
		if (StringUtils.isNotBlank(advTypeParam))
			crit.addEq("advType", advTypeParam);
		if (StringUtils.isNotBlank(categoryRootParam))
			crit.addEq("mainCateId", Long.parseLong(categoryRootParam));
		crit.addEq("postType", "CATE");

		int contentSize = generateService.getViewPmAdvertiseSize(crit).intValue();
		pager.setTotalSize(contentSize);

		viewPmAdvertises = generateService.getViewPmAdvertiseList(pager.getCurrentPage() * pager.getPageRecord(), pager
				.getPageRecord(), crit, null);

		return "listAdv";
	}

	/**
	 * 广告列表 - 行销
	 * 
	 * @return
	 * @throws Exception
	 */
	public String searchIdx() throws Exception {
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isNotBlank(advNameIdxParam))
			crit.addRlike("advName", advNameIdxParam);

		if (StringUtils.isNotBlank(startDateIdxParam))
			crit.addGe("startDate", format.parse(startDateIdxParam + " 00:00:00"));

		if (StringUtils.isNotBlank(endDateIdxParam))
			crit.addLe("endDate", format.parse(endDateIdxParam + " 23:59:59"));

		if (StringUtils.isNotBlank(advStatusIdxParam))
			crit.addEq("advStatus", advStatusIdxParam);
		if (StringUtils.isNotBlank(advTypeIdxParam))
			crit.addEq("advType", advTypeIdxParam);
		crit.addEq("postType", "IDX");
		int contentSize = generateService.getViewPmAdvertiseSize(crit).intValue();
		pager.setTotalSize(contentSize);

		viewPmAdvertises = generateService.getViewPmAdvertiseList(pager.getCurrentPage() * pager.getPageRecord(), pager
				.getPageRecord(), crit, new String[] { "showSeq asc" });
		return "listIdxAdv";
	}

	public void setAdvertises(List<GenerateAdvertise> advertises) {
		this.advertises = advertises;
	}

	public void setAdvIdxTypeMenu(Menu advIdxTypeMenu) {
		this.advIdxTypeMenu = advIdxTypeMenu;
	}

	public void setAdvNameIdxParam(String advNameIdxParam) {
		this.advNameIdxParam = advNameIdxParam;
	}

	public void setAdvNameParam(String advNameParam) {
		this.advNameParam = advNameParam;
	}

	public void setAdvStatusIdxParam(String advStatusIdxParam) {
		this.advStatusIdxParam = advStatusIdxParam;
	}

	public void setAdvStatusMenu(Menu advStatusMenu) {
		this.advStatusMenu = advStatusMenu;
	}

	public void setAdvStatusParam(String advStatusParam) {
		this.advStatusParam = advStatusParam;
	}

	public void setAdvTypeIdxParam(String advTypeIdxParam) {
		this.advTypeIdxParam = advTypeIdxParam;
	}

	public void setAdvTypeMenu(Menu advTypeMenu) {
		this.advTypeMenu = advTypeMenu;
	}

	public void setAdvTypeParam(String advTypeParam) {
		this.advTypeParam = advTypeParam;
	}

	public void setCategoryRoot(List<Category> categoryRoot) {
		this.categoryRoot = categoryRoot;
	}

	public void setCategoryRootParam(String categoryRootParam) {
		this.categoryRootParam = categoryRootParam;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public void setDiffDateMap(Map<Long, String> diffDateMap) {
		this.diffDateMap = diffDateMap;
	}

	public void setEndDateIdxParam(String endDateIdxParam) {
		this.endDateIdxParam = endDateIdxParam;
	}

	public void setEndDateParam(String endDateParam) {
		this.endDateParam = endDateParam;
	}

	public void setGenerateService(GenerateService generateService) {
		this.generateService = generateService;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public void setStartDateIdxParam(String startDateIdxParam) {
		this.startDateIdxParam = startDateIdxParam;
	}

	public void setStartDateParam(String startDateParam) {
		this.startDateParam = startDateParam;
	}

	public void setViewPmAdvertises(List<ViewPmAdvertise> viewPmAdvertises) {
		this.viewPmAdvertises = viewPmAdvertises;
	}
}